package com.cavy.platform.client.service;

//import com.cavy.platform.client.CavyConfig;
//import com.cavy.platform.client.domain.ContainerInfo;
//import com.cavy.platform.client.domain.ContainerRunningException;
//import com.cavy.platform.client.domain.StageDependency;
//import com.github.dockerjava.api.DockerClient;
//import com.github.dockerjava.api.command.CreateContainerCmd;
//import com.github.dockerjava.api.command.StartContainerCmd;
//import com.github.dockerjava.api.command.StopContainerCmd;
//import com.github.dockerjava.api.exception.DockerException;
//import com.github.dockerjava.api.model.LogConfig;
//import com.github.dockerjava.core.DefaultDockerClientConfig;
//import com.github.dockerjava.core.DockerClientBuilder;
//import com.github.dockerjava.core.command.PullImageResultCallback;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.PreDestroy;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.UUID;

/**
 * @author 唐旭
 * @Date 2018/7/26
 */
//@Service
//@Slf4j
public class DockerServiceImpl
//        implements DockerService
{
//    public static final String SPLIT = " ";
//    public static final String TAG = "tag";
//    public static final String FLUENTD_ADDRESS = "fluentd-address";
//    public static final String FLUENTD_ASYNC_CONNECT = "fluentd-async-connect";
//    public static final String CONTAINER_PRE = "cavy-stage-";
//    private DockerClient dockerClient;
//    private DockerEventService eventService;
//
//    @Autowired
//    private CavyConfig config;
//
//    @PostConstruct
//    public void init() {
//        DefaultDockerClientConfig build = DefaultDockerClientConfig.createDefaultConfigBuilder().build();
//        dockerClient = DockerClientBuilder.getInstance(build).build();
//
//        eventService = new DockerEventService(dockerClient);
//        eventService.init();
//    }
//
//    @PreDestroy
//    public void destroy() throws IOException {
//        eventService.destroy();
//        dockerClient.close();
//    }
//
//    /**
//     * 主容器启动并等待容器结束
//     *
//     * @param containerInfo 容器信息
//     * @throws InterruptedException 如果线程被打断,则抛出打断异常
//     */
//    public String runContainerWaitStop(ContainerInfo containerInfo) throws InterruptedException {
//        //创建容器
//        startContainer(containerInfo);
//        return eventService.waitContainerStop(containerInfo.getDockerContainerId());
//    }
//
//    private void startContainer(ContainerInfo containerInfo) {
//        String id;
//        try (CreateContainerCmd containerCmd = getCreateContainerCmd(containerInfo)) {
//            id = containerCmd.exec().getId();
//            containerInfo.setDockerContainerId(id);
//        }
//
//        try (StartContainerCmd startContainerCmd = dockerClient.startContainerCmd(containerInfo.getDockerContainerId())) {
//            startContainerCmd.exec();
//        }
//    }
//
//    private CreateContainerCmd getCreateContainerCmd(ContainerInfo info) {
//        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(info.getImage());
//        containerCmd.withName(CONTAINER_PRE + UUID.randomUUID().toString());
//        if (!StringUtils.isEmpty(info.getCommand())) {
//            containerCmd.withCmd(info.getCommand().split(SPLIT));
//        }
//        if (!StringUtils.isEmpty(info.getUser())) {
//            containerCmd.withUser(info.getUser());
//        }
//        //处理挂载
//        containerCmd.withBinds(info.toBinds());
//        containerCmd.withEnv(info.toEnv());
//        if (!StringUtils.isEmpty(info.getWorkDir())) {
//            containerCmd.withWorkingDir(info.getWorkDir());
//        }
//        containerCmd.withLinks(info.getLinks());
//        //设置label
//        HashMap<String, String> labels = new HashMap<>();
//        labels.put(DockerServiceImpl.CONTAINER_PRE, "");
//        containerCmd.withLabels(labels);
//        //设置日志
//        LogConfig logConfig = new LogConfig();
//        logConfig.setType(LogConfig.LoggingType.FLUENTD);
//        HashMap<String, String> logOpt = new HashMap<>();
//        logOpt.put(TAG, info.getContainerTagName());
//        logOpt.put(FLUENTD_ADDRESS, getFluentdAddress());
//        logOpt.put(FLUENTD_ASYNC_CONNECT, "true");
//        logConfig.setConfig(logOpt);
//        containerCmd.withLogConfig(logConfig);
//        return containerCmd;
//    }
//
//    private String getFluentdAddress() {
//        return config.getLog().getUri();
//    }
//
//    public void stopContainer(ContainerInfo containerInfo) throws ContainerRunningException {
//        if (StringUtils.isEmpty(containerInfo.getDockerContainerId())) {
//            return;
//        }
//        try (StopContainerCmd stopContainerCmd = dockerClient.stopContainerCmd(containerInfo.getDockerContainerId());) {
//            stopContainerCmd.exec();
//        } catch (DockerException e) {
//            log.error("出现错误,错误消息:{},异常类型:{}", e.getMessage(), e.getClass());
//            throw new ContainerRunningException(e);
//        }
//    }
//
//    public void runContainer(StageDependency containerInfo) {
//        startContainer(containerInfo);
//    }
//
//    public void removeContainer(String dockerContainerId) {
//        if (StringUtils.isEmpty(dockerContainerId)) {
//            return;
//        }
//        dockerClient.removeContainerCmd(dockerContainerId).withRemoveVolumes(true).withForce(true).exec();
//    }
//
//    public void printContainers() {
//        eventService.printContainers();
//    }
}
